import akshare as ak
import pandas as pd
import numpy as np

print('bbb')
# 获取上证所有股票代码
sz_stock_list = ak.stock_info_sh_name_code().loc[:,"证券代码"]

print(sz_stock_list)
# 合并两个股票代码列表
stock_list = sz_stock_list

# 分析每只股票的趋势
upward_trend_stocks = []
for stock in stock_list:
    print("======="+stock)
    df = ak.stock_zh_a_hist(symbol=stock, start_date='20210101', end_date='20230630', adjust='qfq')
    print(df)
    df['SMA'] = df['收盘'].rolling(window=50).mean()
    df['EMA'] = df['收盘'].ewm(span=50, adjust=False).mean()
    df['RSI'] = 100 - 100 / (1 + np.exp(-1 * df['收盘'].diff().abs() / df['收盘'].shift(1)))
    print(ak.stock_financial_cash_ths(symbol=stock,indicator='按报告期'))
    df['net_profit'] = ak.stock_financial_cash_ths(symbol=stock,indicator='按报告期')['报表核心指标']
    print("+++++++"+df['net_profit'])

    upward_trend = []
    for i in range(1, len(df)):
        print(df['收盘'][i] > df['收盘'][i - 1] )
        if (df['收盘'][i] > df['收盘'][i - 1] and  # 股价上涨
                df['收盘'][i] > df['SMA'][i] and  # 股价高于 50 日均线
                df['RSI'][i] < 70 and  # RSI 未处于超买状态
                df['net_profit'][i] > df['net_profit'][i - 1]):  # 净利润增长
            upward_trend.append(df['收盘'][i])

    if len(upward_trend) > 0:
        upward_trend_stocks.append((stock, len(upward_trend)))

# 排名前 10 的上涨概率最大的股票
top_10_stocks = sorted(upward_trend_stocks, key=lambda x: x[1], reverse=True)[:10]

# 打印上涨概率最大的前 10 个股票代码和上涨原因
for stock, upward_trend_count in top_10_stocks:
    print(f"股票代码：{stock}")
    print(f"上涨趋势次数：{upward_trend_count}")
    print("上涨原因：")
    print("- 股价上涨")
    print("- 股价高于 50 日均线")
    print("- RSI 未处于超买状态")
    print("- 收入增长")
    print("- 净利润增长")
    print()
